package jp.co.utj.c_walk;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.soap.*;
import java.util.*;

public class UnitecDenkiJAXMClient extends HttpServlet
{
	public UnitecDenkiJAXMClient()
	{
		super();
	}

	public void init(ServletConfig config)
		throws ServletException
	{
		super.init(config);
	}

	public void doPost(HttpServletRequest request,	
		HttpServletResponse response)
		throws IOException, ServletException
	{
		EstimateDataBeans estdata = new EstimateDataBeans();
		request.setAttribute("estdata", estdata);
		estdata.Code = request.getParameter("Code");
		estdata.Count = Integer.parseInt(request.getParameter("Count"));

		try {
			SOAPMessage reply = sendJAXMMessage(estdata.Code, estdata.Count);

			estdata.setResult(getResultFromMessage(reply));
		} catch(SOAPException e) {
			PrintWriter pw = response.getWriter();
			pw.println("<html><body><p>Error in processing or replying to a message</p><p>");
			pw.println(e.toString());
			pw.println("</p></body></html>");
			return;
		}
		request.getRequestDispatcher("/UnitecDenkiJAXMClient.jsp").forward(request, response);
	}

	static String strNamespace
		= "http://unitec-denki.utj.co.jp/schema/2002/";
	static String strHostURL
		= "http://unitec-denki.utj.co.jp:8080/JMWebshopServlet3/JMWebshop";

	static SOAPMessage sendJAXMMessage(String strCode, int iValue)
		throws SOAPException
	{
		SOAPConnectionFactory scf
			= SOAPConnectionFactory.newInstance();
		SOAPConnection connection = scf.createConnection();
		MessageFactory mf = MessageFactory.newInstance();
		SOAPMessage message = mf.createMessage();
		SOAPPart soapPart = message.getSOAPPart();
		SOAPEnvelope env = soapPart.getEnvelope();
		SOAPBody body = env.getBody();

		Name name
			= env.createName("SimpleEstimate", "u", strNamespace);
		SOAPBodyElement bodyElement = body.addBodyElement(name);

		name = env.createName("Code", "u", strNamespace);
		SOAPElement elmCode = bodyElement.addChildElement(name);
		elmCode.addTextNode(strCode);

		name = env.createName("Value", "u", strNamespace);
		SOAPElement elmValue = bodyElement.addChildElement(name);
		elmValue.addTextNode(String.valueOf(iValue));

		SOAPMessage reply = connection.call(message, strHostURL);
		connection.close();
		return reply;
	}

	static String getResultFromMessage(SOAPMessage msg)
		throws SOAPException
	{
		SOAPPart part = msg.getSOAPPart();
		SOAPEnvelope env = part.getEnvelope();
		SOAPBody body = env.getBody();
		if(!body.hasFault())
		{
			Name name = env.createName("SimpleEstimateResponse", "u",
				"http://unitec-denki.utj.co.jp/schema/2002/");
			SOAPBodyElement bodyElement = (SOAPBodyElement)body.
				getChildElements(name).next();
			name = env.createName("SimpleEstimateResult", "u",
				"http://unitec-denki.utj.co.jp/schema/2002/");
			SOAPElement elmResult = (SOAPElement)bodyElement
				.getChildElements(name).next();
			return elmResult.getValue();
		}
		else
		{
			SOAPFault fault = body.getFault();
			Detail detail = fault.getDetail();
			Iterator it = detail.getDetailEntries();
			String strDetail;
			if(it.hasNext())
			{
				SOAPElement elm = (SOAPElement)it.next();
				strDetail = elm.getValue();
			}
			else
			{
				strDetail = "no detail message";
			}
			return "Code = " + fault.getFaultCode()
				+ ", String = " + fault.getFaultString()
				+ ", Actor = " + fault.getFaultActor()
				+ ", Detail = " + strDetail;
		}
	}
}
